package cn.iocoder.yudao.module.hs.service.tray;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hs.controller.admin.tray.vo.TrayPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.tray.vo.TraySaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.tray.TrayDO;
import cn.iocoder.yudao.module.hs.dal.mysql.tray.TrayMapper;
import cn.iocoder.yudao.module.hs.enums.warehouse.WarehouseStatusEnum;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.TRAY_NOT_EXISTS;

/**
 * 托盘信息 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class TrayServiceImpl implements TrayService {

    @Resource
    private TrayMapper trayMapper;

    @Override
    public Long createTray(TraySaveReqVO createReqVO) {
        // 插入
        TrayDO tray = BeanUtils.toBean(createReqVO, TrayDO.class);
        trayMapper.insert(tray);
        // 返回
        return tray.getId();
    }

    @Override
    public void updateTray(TraySaveReqVO updateReqVO) {
        // 校验存在
        validateTrayExists(updateReqVO.getId());
        // 更新
        TrayDO updateObj = BeanUtils.toBean(updateReqVO, TrayDO.class);
        trayMapper.updateById(updateObj);
    }

    @Override
    public void deleteTray(Long id) {
        // 校验存在
        validateTrayExists(id);
        // 删除
        trayMapper.deleteById(id);
    }

    private void validateTrayExists(Long id) {
        if (trayMapper.selectById(id) == null) {
            throw exception(TRAY_NOT_EXISTS);
        }
    }

    @Override
    public TrayDO getTray(Long id) {
        return trayMapper.selectById(id);
    }

    @Override
    public PageResult<TrayDO> getTrayPage(TrayPageReqVO pageReqVO) {
        return trayMapper.selectPage(pageReqVO);
    }

    @Override
    public TrayDO getTray(String trayCode) {
        return trayMapper.selectOne(TrayDO::getCode, trayCode);
    }

    @Override
    public void updateTrayEntryByCode(String code, String rfidCode, Long warehouseId, Long deptId) {
        trayMapper.updateTrayEntryByCode(code, rfidCode, warehouseId, deptId);
    }

    @Override
    public TrayDO getTrayByRfidCode(String rfidCode) {
        return trayMapper.selectOne(TrayDO::getRfidCode, rfidCode);
    }

    @Override
    public void updateTrayOut(Set<String> trayCodes, Long toWarehouseDeptId) {
        LambdaUpdateWrapper<TrayDO> updateWrapper = new LambdaUpdateWrapper<TrayDO>()
                .set(TrayDO::getStatus, WarehouseStatusEnum.SHIP.getStatus())
                .set(TrayDO::getWarehouseId, null)
                .set(toWarehouseDeptId != null, TrayDO::getDeptId, toWarehouseDeptId)
                .in(TrayDO::getCode, trayCodes);
        trayMapper.update(updateWrapper);
    }

    @Override
    public void updateTrayRollback(Set<String> trayCodes, Long warehouseId) {
        LambdaUpdateWrapper<TrayDO> updateWrapper = new LambdaUpdateWrapper<TrayDO>()
                .set(TrayDO::getStatus, WarehouseStatusEnum.ENTRY.getStatus())
                .set(TrayDO::getWarehouseId, warehouseId)
                .in(TrayDO::getCode, trayCodes);
        trayMapper.update(updateWrapper);
    }

    @Override
    public void updateTrayUnbind(Set<String> trayCodes) {
        LambdaUpdateWrapper<TrayDO> updateWrapper = new LambdaUpdateWrapper<TrayDO>()
                .set(TrayDO::getStatus, 3)
                .set(TrayDO::getWarehousingTime, null)
                .set(TrayDO::getRfidCode, null)
                .set(TrayDO::getDeptId, null)
                .in(TrayDO::getCode, trayCodes);
        trayMapper.update(updateWrapper);
    }

}